﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_maintain.TMS_Maintenancerecord
{
    public class TMS_Maintenancerecords : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_Maintenancerecords
    {

        public TMS_Maintenancerecords(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        #region 保养显示
        public async Task<PageResponse<TMS_MaintenancerecordDto>> GetMai(string MaiPeop, string MaiNumber, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_MaintenancerecordDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_MaintenancerecordModel where 1=1 ";
            if (!string.IsNullOrEmpty(MaiPeop))
            {
                sql += $" and MaiPeop like '%{MaiPeop}%'";
            }
            if (!string.IsNullOrEmpty(MaiNumber))
            {
                sql += $" and MaiNumber  like '%{MaiNumber}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_MaintenancerecordDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_MaintenancerecordDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 保养添加
        public async Task<int> MaiAdds(TMS_MaintenancerecordDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_MaintenancerecordModel values ('{info.Id}','{info.Maititel}','{info.MaiNumber}','{info.Maiyuan}','{info.MaiPeop}','{info.Maimiao}','{info.MaiTim}','{info.MaiTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 保养删除
        public async Task<int> DeletMai(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_MaintenancerecordModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

        #region 保养修改
        public async Task<int> UpdateMaiup(TMS_MaintenancerecordDto tMS_Induction)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_MaintenancerecordModel set Maititel='{tMS_Induction.Maititel}',MaiNumber='{tMS_Induction.MaiNumber}',Maiyuan='{tMS_Induction.Maiyuan}',MaiPeop='{tMS_Induction.MaiPeop}',Maimiao='{tMS_Induction.Maimiao}',MaiTim='{tMS_Induction.MaiTim}',MaiTxt='{tMS_Induction.MaiTxt}' where Id='{tMS_Induction.Id}'", null, await GetDbTransactionAsync());
        }
        #endregion
    }
}
